package com.oyb.servlet;

import com.oyb.entity.Admin;
import com.oyb.service.AdminServies;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
@WebServlet(urlPatterns = "/login")
public class LoginServlet extends HttpServlet {

    private AdminServies adminServies = new AdminServies();
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String incode = req.getParameter("incode");
        System.out.println(username);
        System.out.println(password);
        //参数校验

        //接收session
        HttpSession session = req.getSession();
        String code = (String)session.getAttribute("biaoshi");
        //大于60秒

        long createtime = (long) session.getAttribute("time");

        if(System.currentTimeMillis()-createtime >60*1000){
            resp.sendRedirect("login.jsp?status=3");
            return;
        }

      //登陆判断
        if( incode ==null || !incode.equals(code)){
            resp.sendRedirect("login.jsp?status=1");
            return;
        }

        Admin admin = adminServies.login(username,password);
        if (admin != null){
            session.setAttribute("login_user",admin);
            //成功跳转到主页
            resp.sendRedirect("index.jsp");
        }else{
            //失败
            resp.sendRedirect("login.jsp?status=1");
        }

    }
}
